import {
  USER_LOGIN,
  USER_REGISTER,
  CLEAR_USER,
  USER_INFO,
  SAVE_BANK
} from "../constants/account";
import { openGetJSON, getJSON, postJSON } from "../service/request";
import { setCache } from '../utils/cache'
import Taro from "@tarojs/taro";

/**
 * 获取token后发送短信验证码
 * @param {*} params
 */
export const getToken = async params => {
  let obj = {};
  const { status } = params;
  delete params.status;
  if (status === 0) {
    obj = { ...params, module: "CodeService.sendCodeByVcode" };
  } else {
    delete params.inviteid;
    const { data } = await openGetJSON({
      ...params,
      module: "CodeService.getToken"
    });
    if (data && data.state === "000") {
      const { data: c_token } = data;
      obj = { ...params, c_token, module: "CodeService.sendCodeByToken" };
    } else {
      Taro.showToast({
        title: data.msg,
        icon: "none"
      });
      return {};
    }
  }
  const { data: result } = await openGetJSON(obj);
  if (result && result.state === "000") {
    return result;
  } else {
    Taro.showToast({
      title: result.msg,
      icon: "none"
    });
  }
};

/**
 * 登录
 * @param {*} params
 */
export const userLogin = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "UserService.login"
  });
  if (data && data.state === "000") {
    Taro.redirectTo({
      url: "/pages/mine/index"
    });
    return {
      type: USER_LOGIN,
      ...data.data
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
  }
  return { type: USER_LOGIN };
};

/**
 * 注册
 * @param {*} params
 */
export const userRegister = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "UserService.register"
  });
  if (data && data.state === "000") {
    Taro.redirectTo({
      url: "/pages/index/index"
    });
    return {
      type: USER_REGISTER,
      ...data.data
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
  }
  return {
    type: USER_REGISTER
  };
};

/**
 * 用户基本信息
 * @param {*} params
 */
export const getUserInfo = async () => {
  const { data } = await getJSON({ module: "UserService.userinfo" });
  if (data && data.state === "000") {
    return {
      type: USER_INFO,
      ...data.data
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
  }
  return {
    type: USER_INFO
  };
};


export const setUserInfo = async params => {
  const { data } = await postJSON({
    ...params,
    module: "UserService.setUserInfo"
  });
  if (data && data.state === "000") {
    return true;
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
  }
};

/**
 * 绑定银行卡
 * @param {banknum:银行卡号} params
 */
export const checkBankNum = async params => {
  const { data } = await getJSON({ ...params, module: 'AccountService.checkBankNum' })
  return data;
}

/**
 * 保存银行卡信息
 * @param {rname:真实姓名，bancknum-银行卡号，bankcode-银行简称} params
 */
export const saveBank = async params => {
  console.log(params)
  const { data } = await postJSON({ ...params, module: 'AccountService.saveBank' });
  if (data && data.state === '000') {
    return {
      type: SAVE_BANK,
      rname: params.rname,
      banknum:params.banknum
    }
  } else {
    Taro.showToast({
      title: data.msg,
      icon:'none'
    })
    return {
      type:SAVE_BANK
    }
  }
}

/**
 * 申请提现
 * @param {amount:金额} params
 */
export const reqCash = async params => {
  const { data } = await postJSON({ ...params, module: 'AccountService.reqCash' });
  if (data && data.state === '000') {
    return true
  } else {
    Taro.showToast({
      title: data.msg,
      icon: 'none',
      duration:2000
    })
    return false;
  }
}


export const clearUser = () => {
  return {
    type: CLEAR_USER
  };
};
